import { getProductDetail, getProductVOList } from '../../services/goods'
import { dateAddDays3 } from '../../utils/date'
import { priceToYuan } from '../../utils/utils'

const app = getApp()

Page({
  data: {
    unitPrice: 0,
    specImage: '',
    selectSpecTitile: '请选择颜色 规格 租用 天数',
    nodes: [],
    tabIndex: 1,
    goodsInfo: {},
    productId: '',
    specActiveId: -1,
    dayActive: -1,
    specList: [],
    dayList: [],
    skuInfo: {},
    selectedProductSkuInfo: {},
    showBottom: false,
    tagTitle: ''
  },
  onLoad(event) {
    this.setData({ globalData: app.globalData, dayList: app.globalData.playforminfo.dayList })

    this.setData({ productId: event.id })
    this.getProductDetail()
  },
  onShow() {
    my.setNavigationBar({
      title: '商品详情',
      backgroundColor: '#4BA3F9'
    })
  },
  getGoodsDetail(event){
    my.navigateTo({
        url: '/pages/shopDetail/shopDetail?id=' + event.target.dataset.id
      })
  },
  tabClick(event) {
    this.setData({ tabIndex: event.target.dataset.num });
  },
  onClickSku({ target: { dataset: { index, type } } }) {
    if (parseInt(type) == 2) {
      const updateData = {}

      const specList = this.data.specList

      specList[this.data.specActiveId] && (specList[this.data.specActiveId].popupactive = false)

      if (this.data.specActiveId == index) {//再次点击选中的，取消选中
        updateData.specActiveId = -1
      } else {
        updateData.specActiveId = index
        specList[index].popupactive = true
      }
      
      updateData.specList = [...specList]

      this.setData(updateData)

      this.oncalcSku2()
    }
  },
  changeday({ target: { dataset: { value } } }) {
    if (value === this.data.dayActive) {
      this.setData({ dayActive: -1 })
    } else {
      this.setData({ dayActive: value })
    }

    this.oncalcSku2()
  },
  oncalcSku2() {
    let specImage = this.data.goodsInfo.mainImage
    let unitPrice = this.data.goodsInfo.cheapUnitPrice
    let selectSpecTitile = '请选择规格 租用天数'
    let that = this
    
    const { skuInfo: { attribute_info: skus }, specList, specActiveId, dayActive } = this.data

    if (specActiveId >= 0 && dayActive >= 0) {//全部选择
      const selectedProductSkuInfo = skus.find(item => item.name == specList[specActiveId].name)

      unitPrice = priceToYuan(selectedProductSkuInfo.rent)

      selectSpecTitile = `已选择${selectedProductSkuInfo.name} ${dayActive}天租用`

      this.setData({ selectedProductSkuInfo })
    } else if (specActiveId < 0) {//颜色未选择 规格选择
      selectSpecTitile = '请选择租用规格'
    } else if (dayActive <= 0) {//颜色 规格 选择
      selectSpecTitile = '请选择租用天数'
    }

    this.setData({ specImage: specImage })
    this.setData({ unitPrice: unitPrice })
    this.setData({ selectSpecTitile: selectSpecTitile })
  },

  onPopupClose() {
    this.setData({
      showBottom: false
    });
  },
  onPopupSure() {
    if (this.data.specActiveId < 0) {
      my.showToast({
        type: 'fail',
        content: '请选择规格',
        duration: 2000,
      });
      return
    }
    if (this.data.dayActive <= 0) {
      my.showToast({
        type: 'fail',
        content: '请选择租用天数',
        duration: 2000,
      });
      return
    }
    if (this.data.selectedProductSkuInfo.stock <= 0) {
      my.showToast({
        type: 'fail',
        content: '库存不足',
        duration: 2000,
      });
      return
    }
    let pushdata = 'id=' + this.data.productId
    pushdata += '&skuId=' + this.data.selectedProductSkuInfo.name
    pushdata += '&rentDay=' + this.data.dayActive
    pushdata += '&skuImage=' + this.data.specImage
    pushdata += '&specName=' + this.data.selectSpecTitile
    pushdata += '&goodsName=' + this.data.goodsInfo.productName

    my.navigateTo({
      url: '/pages/orderSure/orderSure?' + pushdata
    })
  },
  onPopupOpen() {
    this.setData({
      showBottom: true,
    })
  },
  goHome() {
    my.switchTab({
      url: '/pages/home/home',
    })
  },
  goCustomer() {
    my.switchTab({
      url: '/pages/customer/customer',
    })
  },
  async getProductDetail() {
    const { code, data } = await getProductDetail({
      id: this.data.productId
    })

    if (code != 0) return false
    
    const { attribute_info, attribute_name } = data

    const goodsInfo = {
      cheapUnitPrice: priceToYuan(Math.min(...attribute_info.map(item => item.rent))),
      deliveryType: 1,
      isPurchase: false,
      isRenewal: true,
      mainImage: data.pics[0],
      productDetail: data.detail,
      productImageList: data.pics,
      productName: data.name,
      productSales: Math.ceil(Math.random(100))
    }

    this.setData({ goodsInfo })

    this.getProductSku({ attribute_info, attribute_name })
    this.getProductVOList()
    this.oncalcSku2()
  },
  getProductSku({ attribute_info, attribute_name }) {
    this.setData({
      skuInfo: { attribute_name, attribute_info },
      specList: attribute_info.map(item => ({ name: item.name, isdisable: item.stock <= 0 }))
    })
  },
  async getProductVOList() {
    const { code, data } = await getProductVOList({
      id: this.data.productId
    })

    if (code != 0) return false

    this.setData({ productVOList: data.list.map(item => ({
      productId: item.id,
      productName: item.name,
      productRentPrice: priceToYuan(Math.min(...item.attribute_info.map(item => item.rent))),
      productRentDay: this.data.globalData.playforminfo.startRentDay,
      mainImage: item.avatar
    })) })
  },
})




